package com.cmpe.av;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cmpe.av.logic.UserLogic;
import com.cmpe.av.logic.ReportLogic;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Login() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {


			doPost(request, response);
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub


		UserLogic user;
		ReportLogic report;
		String userName = request.getParameter("name");
		String passWord = request.getParameter("password");
		String strErrMsg = null;
		HttpSession session = request.getSession();
		int userID = -1;
		// response.
		if (request.getParameter("button").equals("Login")) {
			try {
				user = new UserLogic();
				userID = user.getUserID(userName, passWord);
				
				if (userID != -1 && user.getActiveInfo(userID)) {
					session.setAttribute("userName", userName);
					session.setAttribute("loggedin", true);
					session.setAttribute("userID", userID);

					response.setHeader("Cache-Control","no-cache"); 
					response.setHeader("Pragma","no-cache"); 
					response.setDateHeader ("Expires", 0); 
					response.setHeader("Cache-Control", "no-store");

					request.getRequestDispatcher("homeMember.jsp").forward(
							request, response);
				} else {
					request.setAttribute("invalidLogin", true);
					request.getRequestDispatcher("/default.jsp").forward(request,
							response);	
				}
			} 

			catch (Exception e) {
				strErrMsg = "Unable to validate user / password in database";
				System.out.println(e);
			}
		} 
		
		else if (request.getParameter("button").equals("Forgot Password?")) {
			try {
				user = new UserLogic();
				report = new ReportLogic();
				boolean nameExist = false;
				String usrName = request.getParameter("name");
				nameExist = user.isInDb(usrName);
				if(!nameExist)
				{
					request.setAttribute("userNotExists", true);
					request.getRequestDispatcher("/default.jsp").forward(request,
							response);
					return;
				}
				String mail = user.getMail(usrName);
				String password = user.getPassword(usrName);
				String mailSubject = "Password";
				report.sendMail(mail,mailSubject, "Your password is "+password+".");
				request.setAttribute("forgotPassword", true);
				request.getRequestDispatcher("/default.jsp").forward(request,
						response);	
			
			} 

			catch (Exception e) {
				strErrMsg = "Unable to find password in database";
				System.out.println(e);
			}

		} 

		else if (request.getParameter("button").equals("Register")) {
			response.sendRedirect("default.jsp");

		} 

		else {
			response.sendRedirect("default.jsp");
		}
	}
}
